﻿using UnityEngine;
using System.Collections;

public class Enemy : MonoBehaviour {

	public float delayToRay = 2f;
	private float timer;
	public EnemyRaycast[] ray;
	
	private float minDist;
	public short minIndex = 0;
	private bool foundMin = false;


	public Cannon[] cannon;



	void Start () {
		timer = delayToRay;
	}
	


	void Update () {
		timer -= Time.deltaTime;
		if(timer < 0f){
			minDist = ray[minIndex].LockOn();
			for(short i = 1; i < ray.Length; i++){
				CheckDistance(minDist, ray[i].LockOn());
				if(foundMin){
					minIndex = i;
					foundMin = false;
				}
			}
			timer = delayToRay;
		}
		cannon [minIndex].work = true;

	}



	private void CheckDistance(float a, float b){
		if(b < a){
			minDist = b;
			foundMin = true;
		}
	}
}
